from django import forms
from django.contrib.auth.models import User

from zlauth.models import CaptchaModel


class ResetPasswordForm(forms.Form):
    email = forms.EmailField(error_messages={"required": "邮箱不能为空", "invalid": "邮箱格式不正确"})
    password = forms.CharField(widget=forms.PasswordInput)
    password2 = forms.CharField(
        widget=forms.PasswordInput,
        error_messages={"required": "请再次输入密码"}
    )

    captcha = forms.CharField(max_length=6, min_length=6, required=False, error_messages={"required": "验证码不能为空"})

    def clean_captcha(self):
        captcha = self.cleaned_data.get('captcha')
        email = self.cleaned_data.get('email')
        if not captcha:
            raise forms.ValidationError("验证码不能为空")

        captcha_model = CaptchaModel.objects.filter(email=email, captcha=captcha).first()
        if not captcha_model:
            raise forms.ValidationError("验证码错误或已过期")
        captcha_model.delete()
        return captcha

    def clean(self):
        cleaned_data = super().clean()
        password = cleaned_data.get("password")
        password2 = cleaned_data.get("password2")
        if password and len(password) < 6:
            self.add_error("password", "密码不能少于6个字符")
        if password and password2 and password != password2:
            self.add_error("password2", "两次密码不一致")
        return cleaned_data


class RegisterForm(forms.Form):
    username = forms.CharField(max_length=20, min_length=2,
                               error_messages={"required": "用户名不能为空",
                                               "min_length": "用户名不能少于2个字符",
                                               "max_length": "用户名不能多于20个字符"
                                               }
                               )
    email = forms.EmailField(error_messages={"required": "邮箱不能为空", "invalid": "邮箱格式不正确"})
    password = forms.CharField(widget=forms.PasswordInput)
    password2 = forms.CharField(
        widget=forms.PasswordInput,
        error_messages={"required": "请再次输入密码"}
    )

    captcha = forms.CharField(max_length=6, min_length=6, required=False, error_messages={"required": "验证码不能为空"})

    def clean_email(self):
        email = self.cleaned_data.get('email')
        if User.objects.filter(email=email).exists():
            raise forms.ValidationError("邮箱已存在")
        return email

    def clean_captcha(self):
        captcha = self.cleaned_data.get('captcha')
        email = self.cleaned_data.get('email')
        if not captcha:
            raise forms.ValidationError("验证码不能为空")

        captcha_model = CaptchaModel.objects.filter(email=email, captcha=captcha).first()
        if not captcha_model:
            raise forms.ValidationError("验证码错误或已过期")
        captcha_model.delete()
        return captcha

    def clean(self):
        cleaned_data = super().clean()
        password = cleaned_data.get("password")
        password2 = cleaned_data.get("password2")
        if password and len(password) < 6:
            self.add_error("password", "密码不能少于6个字符")
        if password and password2 and password != password2:
            self.add_error("password2", "两次密码不一致")
        return cleaned_data


class LoginForm(forms.Form):
    email = forms.EmailField(error_messages={"required": "邮箱不能为空", "invalid": "邮箱格式不正确"})
    password = forms.CharField(widget=forms.PasswordInput)
    remember = forms.IntegerField(required=False)
